import { createRouter, createWebHashHistory, NavigationGuardNext, RouteLocationNormalized, Router, RouteRecordRaw } from 'vue-router';
import { MatchInterface, RouterData, } from './types/Interface';


/* 超级签应用 */
const SuperList: RouteRecordRaw = {
  name: 'SuperList', path: "/superlist", meta: { title: '超级签应用', name: '超级签列表' },
  component: () => import('@/views/AppSuper/SuperList.vue')
}
/* 上传超级签 */
const SuperUpload: RouteRecordRaw = {
  name: 'SuperUpload', path: "/superupload", meta: { title: '超级签应用', name: '上传超级签' },
  component: () => import('@/views/AppSuper/SuperUpload.vue')
}


/* 苹果分发列表 */
const IosList: RouteRecordRaw = {
  name: 'IosList', path: "/ioslist", meta: { title: '苹果分发应用', name: '苹果分发列表' },
  component: () => import('@/views/IosDistri/IosList.vue')
}
/* 苹果分发上传 */
const IosUpload: RouteRecordRaw = {
  name: 'IosUpload', path: "/iosupload", meta: { title: '苹果分发应用', name: '上传苹果分发 ' },
  component: () => import('@/views/IosDistri/IosUpload.vue')
}


/* 安卓分发列表 */
const AppList: RouteRecordRaw = {
  name: 'AppList', path: "/applist", meta: { title: '安卓分发应用', name: '安卓分发列表' },
  component: () => import('@/views/ApkDistri/AppList.vue')
}
/* 安卓分发上传 */
const AppUpload: RouteRecordRaw = {
  name: 'AppUpload', path: "/appupload", meta: { title: '安卓分发应用', name: '上传安卓分发' },
  component: () => import('@/views/ApkDistri/AppUpload.vue')
}


/* 免签分发列表 */
const MiaoList: RouteRecordRaw = {
  name: 'MiaoList', path: "/miaolist", meta: { title: '免签分发应用', name: '免签分发列表' },
  component: () => import('@/views/MiaoList/MiaoList.vue')
}

/* 免签分发上传 */
const MiaoUpload: RouteRecordRaw = {
  name: 'MiaoUpload', path: "/miaoupload", meta: { title: '免签分发应用', name: '上传免签分发' },
  component: () => import('@/views/MiaoList/MaoUpload.vue')
}



/* 账号列表 */
const CerList: RouteRecordRaw = {
  name: 'CerList', path: "/cerlist", meta: { title: '开发账号管理', name: '账号列表' },
  component: () => import('@/views/CerManage/CerList.vue')
}
/* 上传账号 */
const CerUpload: RouteRecordRaw = {
  name: 'CerUpload', path: "/cerupload", meta: { title: '开发账号管理', name: '提交账号 ' },
  component: () => import('@/views/CerManage/CerUpload.vue')
}


/* 用户列表 */
const UserList: RouteRecordRaw = {
  name: 'UserList', path: "/userlist", meta: { title: '用户模块管理', name: '用户列表 ' },
  component: () => import('@/views/UserManage/UserList.vue')
}

/* 下载域名 */
const DnsList: RouteRecordRaw = {
  name: 'DnsList', path: "/dnsList", meta: { title: '用户模块管理', name: '域名配置 ' },
  component: () => import('@/views/DnsConfig/DnsConfig.vue')
}


const Index: RouteRecordRaw = {
  name: 'Index', path: "/index", meta: { title: '首页', name: '首页' },
  component: () => import('@/views/Index/Index.vue')
}


/* 匹配router */
const match: MatchInterface = {
  Index, SuperList, SuperUpload,
  IosList, IosUpload, AppList,
  AppUpload, CerList, CerUpload,
  UserList, DnsList, MiaoUpload, MiaoList,
};

/* 动态路由 */
export const DynamicRouting = (): RouteRecordRaw[] => {
  const childrenRoutes: RouteRecordRaw[] = []
  const routerdata: RouterData | null = JSON.parse(localStorage.getItem('userdata') as string);
  if (routerdata) {
    routerdata.router.forEach((item) => {
      childrenRoutes.push(match[item.name as string]);
    });
  };
  return childrenRoutes;
}


function InitRouterList(): RouteRecordRaw {
  return {
    path: '/', name: 'Home',
    component: () => import('@/components/Home/Home.vue'),
    children: [
      {
        name: 'Index', path: "/index", meta: { title: '首页', name: '首页' },
        component: () => import('@/views/Index/Index.vue')
      },
      ...DynamicRouting()
    ]
  }
}


const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录', name: '登录' },
    component: () => import('@/views/Login/Login.vue')
  },
  {
    path: '/404',
    name: '404',
    meta: { title: '404', name: '404' },
    component: () => import('@/views/404/404.vue'),
  },
  InitRouterList()
]



/* 路由实例 */
const router: Router = createRouter({
  history: createWebHashHistory(),
  routes
});


export const addRoute = () => {
  router.addRoute(InitRouterList())
}

/* 路由守卫 */
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  document.title = (window as any).$TITLE as string
  if (to.matched.length !== 0) {
    if (to.path !== '/login') {
      if (!localStorage.getItem('token')) {
        next({ path: "/login" });
      } else {
        addRoute()
        next()
      }
    } else {
      next()
    };
  } else {
    next({ path: "/404" });
  }
});


export default router
